//
//  HBChapterRequest.m
//  HeartBeat
//
//  Created by karong on 2023/8/31.
//

#import "HBChapterRequest.h"

@implementation HBChapterRequest
- (void)refreshDataSuccess:(回调一个数组)success failed:(失败回调)failed {
    if (self.loading) {
        return;
    }
    self.loading = YES;
    self.haveMore = YES;
    self.page = 1;
    [HBRequest loadNovelChapterListWithBookId:self.novel_id sort_type:self.sort_type page:self.page success:^(HBChaptersModel * obj) {
        self.totalCount = obj.total;
        self.chapters = obj;
        [self loadSuccess:success withArray:obj.items];
        
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        failed(response, error, responseObject);
        self.loading = NO;
    }];
}

- (void)loadMoreDataSuccess:(回调一个数组)success failed:(失败回调)failed {
    if (self.loading && !self.haveMore) {
        return;
    }
    [HBRequest loadNovelChapterListWithBookId:self.novel_id sort_type:self.sort_type page:self.page success:^(HBChaptersModel * obj) {
        self.totalCount = obj.total;
        self.chapters = obj;
        [self loadSuccess:success withArray:obj.items];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        failed(response, error, responseObject);
        self.loading = NO;
    }];
}

- (void)loadSuccess:(回调一个数组)success withArray:(NSArray *)array {
    self.haveMore = (self.totalCount - self.page * 10) > 0;
    self.page++;
    self.loading = NO;
    success(array);
}
@end
